using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Xpo;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Data.Filtering;
using Ascent.QiPei.Module.BusinessObjects.Sales;
using DevExpress.ExpressApp.SystemModule;

namespace Ascent.QiPei.Module.Controllers
{
    public partial class ViewController1 : ViewController
    {
        public ViewController1()
        {
            InitializeComponent();
            RegisterActions(components);
        }

        private void simpleAction1_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            
            var bo1 = ObjectSpace.FindObject<BusinessObjects.BusinessObject1>(new BinaryOperator("Name", "bo1"));
            if (bo1 == null)
            {
                bo1 = ObjectSpace.CreateObject<BusinessObjects.BusinessObject1>();
                bo1.Name = "bo1";
            }
            var bo2 = ObjectSpace.FindObject<BusinessObjects.BusinessObject1>(new BinaryOperator("Name", "bo2"));
            if (bo2 == null)
            {
                bo2 = ObjectSpace.CreateObject<BusinessObjects.BusinessObject1>();
                bo2.Name = "bo2";
            }
            ObjectSpace.CommitChanges();

            Frame.GetController<RefreshController>().RefreshAction.DoExecute();

            var s = this.View.CurrentObject.ToString();
            
        }

        private void simpleAction2_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            var objectSpace = Application.CreateObjectSpace();
            var obj = objectSpace.CreateObject(typeof(SaleOrder));
            var svp = new ShowViewParameters();
            svp.CreatedView = Application.CreateDetailView(objectSpace, Application.FindDetailViewId(typeof(SaleOrder)), true, obj);
            svp.TargetWindow = TargetWindow.Default;
            svp.Context = TemplateContext.View;
            svp.CreateAllControllers = true;
            svp.NewWindowTarget = NewWindowTarget.Default;
            //(svp.CreatedView as DetailView).ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;

            Application.ShowViewStrategy.ShowView(svp, new ShowViewSource(null, null));
            //Application.CreateController<NewObjectViewController>()
            //Frame.GetController<NewObjectViewController>().NewObjectAction.
            
        }

        private void simpleAction3_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            //if(this.View is DetailView)
            //    (this.View as DetailView).ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.View;
            this.View.Model.AllowEdit = ! this.View.Model.AllowEdit;
            this.View.LoadModel();
        }

        private void ViewController1_Activated(object sender, EventArgs e)
        {
            
        }

        private void ViewController1_ViewControlsCreated(object sender, EventArgs e)
        {

        }

        private void simpleAction4_Execute(object sender, SimpleActionExecuteEventArgs e)
        {
            Frame.GetController<WindowTemplateController>().UpdateWindowStatusMessage();
        }
    }
}
